package com.yuexunit.finance.news.action.v1.dto;

import java.util.List;

import com.yuexunit.finance.news.action.v1.model.NewsDetail;
import com.yuexunit.finance.news.action.v1.model.NewsSummary;
import com.yuexunit.finance.news.action.v1.model.NewsDetail.Content;
import com.yuexunit.finance.news.entity.NewsCategoryEntity;
import com.yuexunit.finance.news.entity.NewsContentEntity;
import com.yuexunit.finance.news.entity.NewsEntity;
import com.yuexunit.finance.news.service.NewsCategoryService;
import com.yuexunit.finance.news.service.NewsService;
import com.yuexunit.inject.ComponentFactory;

/**
 * Created by HuangYH on 2015/9/22.
 */
public class NewsCastor {

	private static NewsCategoryService newsCategoryService = ComponentFactory.loadClass(NewsCategoryService.class);

	private static void fromNewsEntity(NewsEntity entity, NewsSummary summary) {
		summary.setNewsId(entity.getNewsId());
		summary.setNewsCategoryId(entity.getNewsCategoryId());
		summary.setNewsTitle(entity.getNewsTitle());
		summary.setSummary(entity.getSummary());
		summary.setImageUuid(entity.getImageUuid());
		summary.setNewsSource(entity.getNewsSource());
		summary.setRecommend(String.valueOf(entity.getRecommend()));
		summary.setNewsSourceLink(entity.getNewsSourceLink());
		summary.setPublisherId(entity.getPublisherId());
		summary.setPublisher(entity.getPublisher());
		summary.setIssueDate(entity.getUpdateDateTime());
	}

	public static NewsSummary summaryFromNewsEntity(NewsEntity entity) {
		NewsSummary summary = new NewsSummary();
		fromNewsEntity(entity, summary);

		NewsCategoryEntity category = newsCategoryService.inquireNewsCategory(entity.getNewsCategoryId());
		summary.setNewsCategoryName(category.getCategoryName());
		return summary;
	}

	public static NewsDetail detailFromNewsEntity(NewsEntity entity) {
		NewsDetail detail = new NewsDetail();
		fromNewsEntity(entity, detail);
		NewsCategoryEntity category = newsCategoryService.inquireNewsCategory(entity.getNewsCategoryId());
		detail.setNewsCategoryName(category.getCategoryName());
		
		// 获取新闻内容
		NewsService newsService = ComponentFactory.loadClass(NewsService.class);
		List<NewsContentEntity> contentEntityList = newsService.inquireNewsContentList(detail.getNewsId());
		for (NewsContentEntity contentEntity : contentEntityList) {
			Content c = new Content();
			c.setNewsContentId(contentEntity.getNewsContentId());
			c.setContent(contentEntity.getContent());
			c.setFileUuid(contentEntity.getFileUuid());
			c.setSequence(contentEntity.getSequence());
			detail.getContents().add(c);
		}
		return detail;
	}
}
